// As soon as the page has finished loading, initialize ineo!
window.onload = ineoGlobalInit;

/**
* Global initialization - set up event handlers, etc.
*/
function ineoGlobalInit()
{
	// Global Event Handlers:
	document.getElementById('ineo-quicksearch-form').onsubmit = function() { ineoQuickSearch( document.getElementById('ineo-quicksearch-words').value ); return false; }
	
	// Article View Event Handlers:
	if( ineoModule == 'article' && ineoAction == 'view' )
	{
		var del; var create; var view; var opts;
		
		if( del = document.getElementById('ineo-delete-article') )
		{
			del.onclick = function() { return ineoConfirmDeleteArticle(); }
		}
		
		if( create = document.getElementById('ineo-topic-create') )
		{
			create.onclick = function() { ineoCreateTopic( document.getElementById('article_id').value ); return false; }
		}
		
		if( view = document.getElementById('ineo-topic-view') ) 
		{
			if( useAjaxPreview == 1 )
			{
				view.onclick = function() { ineoShowTopic( document.getElementById('article_tid').value , document.getElementById('article_id').value ); return false; }
			}
		}
		
		if( opts = document.getElementById('ineo-rate-options') ) 
		{
			document.getElementById('ineo-rate-1').onclick = function() { ineoRateArticle( document.getElementById('article_id').value , 1 );   return false; }
			document.getElementById('ineo-rate-2').onclick = function() { ineoRateArticle( document.getElementById('article_id').value , 2 );   return false; }
			document.getElementById('ineo-rate-3').onclick = function() { ineoRateArticle( document.getElementById('article_id').value , 3 );   return false; }
			document.getElementById('ineo-rate-4').onclick = function() { ineoRateArticle( document.getElementById('article_id').value , 4 );   return false; }
			document.getElementById('ineo-rate-5').onclick = function() { ineoRateArticle( document.getElementById('article_id').value , 5 );   return false; }
		}
	}
	
	// Category View Event Handlers:
	if( ineoModule == 'category' && ineoAction == 'cat' )
	{
		ineoAddReorderEvents();
	}
}

/**
* Quick Search Event Handler!
*/
function ineoQuickSearch( words )
{
	ineoAjaxReplace('op=search&q=' + words,'dyncontent',ams_lang_searching);
}

var reorder_orders = Array();
reorder_orders['article_title']       = 'DESC';
reorder_orders['article_desc']        = 'DESC';
reorder_orders['article_views']       = 'DESC';
reorder_orders['article_date']        = 'DESC';

function ineoAddReorderEvents()
{
	var title = document.getElementById('article_title');
	var desc  = document.getElementById('article_desc');
	var views = document.getElementById('article_views');
	var date  = document.getElementById('article_date');
	
	ineoAddReorderEventHandler( title , 'article_title' );
	ineoAddReorderEventHandler( desc  , 'article_description' );
	ineoAddReorderEventHandler( views , 'article_views' );
	ineoAddReorderEventHandler( date  , 'article_created_date' );
}

function ineoAddReorderEventHandler( element , eleId )
{
	/**
	* I'm not quite sure if it's supposed to be hand, or pointer... so i'ma set both! :D
	*/
	element.cursor = 'hand';
	element.cursor = 'pointer';
	element.style.cursor = 'hand';
	element.style.cursor = 'pointer';

	element.onclick = function () {
		var order = '';
	
		if( reorder_orders[eleId] == 'DESC' )
		{
			order = 'ASC';
			reorder_orders[eleId] = 'ASC';
		}
		else
		{
			order = 'DESC';
			reorder_orders[eleId] = 'DESC';
		}

		ineoDoArticleReorder( eleId , order );
	}
}

function ineoDoArticleReorder( order_by , order_dir )
{
	var myEvent = function() {
		
		if( ineoAjaxReady() )
		{
			ineoAjaxReplaceEvent('category-articles-div', ineoAjaxResponseText() );
			ineoAddReorderEvents();
		}
	}
	
	ineoAjaxEvent("op=reorder_articles&category=" + cat + "&order_by=" + order_by + "&order_dir=" + order_dir + "&page=" + startpage, myEvent, ams_lang_reordering);
}

function ineoCreateTopic( topic_id )
{
	ineoAjaxReplace('op=create_topic&id=' + topic_id , 'ineo-topic', ams_lang_creating_topic);
}

function ineoShowTopic( topic_id , article_id )
{
	var myEvent = function() {
		if( ineoAjaxReady() )
		{
			var art = document.getElementById('article_content');
			art.style.display = 'none';
	
			ineoAjaxReplaceEvent('topic_content',ineoAjaxResponseText());
		}
	}	
	
	ineoAjaxEvent('op=topic_quickview&aid=' + article_id + '&tid=' + topic_id,myEvent,ams_lang_loading_topic);
}

function ineoReturnToArticle()
{
	var art = document.getElementById('article_content');
	art.style.display = 'block';
	var dyn = document.getElementById('topic_content');
	dyn.style.display = 'none';
}

function ineoConfirmDeleteArticle( URL )
{
	return confirm( ams_lang_confirm_delete );
}

function ineoRateArticle( article , score )
{
	ineoAjaxReplace('op=rate_article&artid=' + article + '&rating=' + score,'article_rating',ams_lang_rating);
}
